package org.colafries.admin.service.impl.index;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.colafries.admin.entity.domain.index.AtomicMetric;
import org.colafries.admin.entity.vo.index.AtomicMetricReqVO;
import org.colafries.admin.exceptions.ServiceException;
import org.colafries.admin.exceptions.enums.ErrorCodeEnum;
import org.colafries.admin.mapper.index.AtomicMetricMapper;
import org.colafries.admin.service.impl.IBaseServiceImpl;
import org.colafries.admin.service.index.IAtomicMetricService;
import org.colafries.common.constants.Constants;
import org.colafries.common.snowflake.SnowFlake;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 原子指标表 服务实现类
 * </p>
 *
 * @author Ann
 * @since 2024-06-03
 */
@Service("AtomicMetricServiceImpl")
public class IAtomicMetricServiceImpl extends IBaseServiceImpl<AtomicMetricMapper, AtomicMetric, AtomicMetricReqVO>
        implements IAtomicMetricService {

    @Override
    public List<AtomicMetric> listAll(AtomicMetricReqVO vo) {
        LambdaQueryWrapper<AtomicMetric> queryWrapper = new LambdaQueryWrapper<>();
        if (vo.getLogicTableCode() != null) {
            queryWrapper.eq(AtomicMetric::getLogicTableCode, vo.getLogicTableCode());
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public int add(AtomicMetricReqVO vo) {
        // 新增原子指标
        Long atomicMetricCode = SnowFlake.nextId();
        AtomicMetric atomicMetric = AtomicMetric.builder()
                .code(atomicMetricCode)
                .version(Constants.VERSION_FIRST)
                .dataDomainCode(vo.getDataDomainCode())
                .logicTableCode(vo.getLogicTableCode())
                .field(vo.getField())
                .alias(vo.getAlias())
                .description(vo.getDescription())
                .dataType(vo.getDataType())
                .expression(vo.getExpression())
                .createUser(vo.getCreateUser())
                .updateUser(vo.getUpdateUser())
                .build();
        return baseMapper.insert(atomicMetric);
    }

    @Override
    public int update(AtomicMetricReqVO vo) {
        Long atomicMetricCode = vo.getCode();
        LambdaQueryWrapper<AtomicMetric> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AtomicMetric::getCode, atomicMetricCode);
        AtomicMetric oldAtomicMetric = baseMapper.selectOne(queryWrapper);
        if (oldAtomicMetric == null) {
            log.warn("更新异常：原子指标不存在,code:" + atomicMetricCode);
            throw new ServiceException(ErrorCodeEnum.GL999999, "更新异常：原子指标不存在,code:" + atomicMetricCode);
        }
        AtomicMetric newAtomicMetric = AtomicMetric.builder()
                .code(atomicMetricCode)
                .version(oldAtomicMetric.getVersion() + 1)
                .dataDomainCode(vo.getDataDomainCode())
                .logicTableCode(vo.getLogicTableCode())
                .field(vo.getField())
                .alias(vo.getAlias())
                .description(vo.getDescription())
                .dataType(vo.getDataType())
                .expression(vo.getExpression())
                .updateUser(vo.getUpdateUser())
                .build();
        return baseMapper.update(newAtomicMetric, queryWrapper);
    }

    @Override
    public int delete(AtomicMetricReqVO vo) {
        Long atomicMetricCode = vo.getCode();
        LambdaQueryWrapper<AtomicMetric> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AtomicMetric::getLogicTableCode, vo.getLogicTableCode()).eq(AtomicMetric::getCode, atomicMetricCode);
        AtomicMetric atomicMetric = baseMapper.selectOne(queryWrapper);
        if (atomicMetric == null) {
            log.warn("删除异常：原子指标不存在,code:" + atomicMetricCode);
            throw new ServiceException(ErrorCodeEnum.GL999999, "删除异常：原子指标不存在,code:" + atomicMetricCode);
        }
        return baseMapper.delete(queryWrapper);
    }

    /**
     * queryByCode
     * @param code
     * @return org.colafries.admin.entity.domain.index.AtomicMetric
     * @Author anning
     * @Date 2024/6/12
     */
    @Override
    public AtomicMetric queryByCode(Long code) {
        // 查询原子指标
        return baseMapper.selectOne(Wrappers.<AtomicMetric>lambdaQuery().eq(AtomicMetric::getCode, code));
    }

}
